﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.SM;
using System;
using System.Collections.Generic;
using UFIDA.U9.ISV.SM;

namespace dz.inner.jky.api.service
{
    internal class RMAService : BaseService
    {
        public U9CResult Add(DzRMA dto)
        {
            return util.U9Invoke(__Add, dto, dto.Org);
        }

        public void Submit(CommonDoc doc)
        {
            SubmitRMASRV srv = new SubmitRMASRV();

            DocKeyDTO dto = new DocKeyDTO();
            dto.DocID = doc.ID;
            dto.DocNO = doc.DocNo;

            srv.RMAKeys = new List<DocKeyDTO>() { dto };

            srv.Do();
        }

        public void Audit(CommonDoc doc)
        {
            AuditRMASRV srv = new AuditRMASRV();

            DocKeyDTO dto = new DocKeyDTO();
            dto.DocID = doc.ID;
            dto.DocNO = doc.DocNo;

            srv.RMAKeys = new List<DocKeyDTO>() { dto };

            srv.Do();

        }
        public U9CResult __Add(DzRMA dto)
        {
            U9CResult rtn = new U9CResult();

            try
            {
                CreateRMASRV sv = new CreateRMASRV();

                sv.RMADTOs = Convert(dto);

                List<DocKeyDTO> aa = sv.Do();

                rtn.IsSuccess = true;
                rtn.DocNo = aa[0].DocNO;
                rtn.Id = aa[0].DocID;

            }
            catch (Exception ex)
            {
                rtn.IsSuccess = false;
                logUtils.Error(ex);
                rtn.Message = ex.Message;
            }

            return rtn;
        }

        private List<RMADTO> Convert(DzRMA dto)
        {
            ObjectCopyUtil copyUtil = new ObjectCopyUtil();

            RMADTO obj = new RMADTO();

            dynamic c = (dynamic)obj;
            copyUtil.CopyProperty(dto, ref c);

            RMADTO dto1 = (RMADTO)c;

            if (dto1.RMAAddresses == null) dto1.RMAAddresses = new List<RMAAddresseDTO>();
            if (dto1.RMAFees == null) dto1.RMAFees = new List<RMAFeeDTO>();
            if (dto1.RMAContacts == null) dto1.RMAContacts = new List<RMAContactDTO>();

            List<RMADTO> rtn = new List<RMADTO>();

            rtn.Add(dto1);

            return rtn;
        }
    }
}
